/* This file has been modified by Rohit Khare, 10/31/94 */
/*
 * Copyright (c) 1992 Athena Design, Inc. and David Pollak  
 *
 *			ALL RIGHTS RESERVED       
 * 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the company (Athena Design) nor the names of its 
 *    employees may be used to endorse or promote products derived from this 
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ATHENA DESIGN ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ATHENA DESIGN BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */


#import <appkit/publicWraps.h>
#import <appkit/Font.h>
#import <dpsclient/psops.h>
#import <appkit/ActionCell.h>
#import <appkit/FontManager.h>
#import <appkit/Application.h>

#import "FontWell.h"
#import "DPFontPanel.h"

inline double max(double n1,double n2)
{
	if (n1 > n2) return n1;
	return n2;
	}
	
@implementation FontWell

- initFrame:(const NXRect *)re
{
	
	[super initFrame:re];
	[self setCell:[[ActionCell alloc] init]];
	theFont = [Font userFontOfSize:12.0 matrix:NX_IDENTITYMATRIX];
	[self setFlipped:NO];
	return self;
	}
	
- drawSelf:(const NXRect *)re :(int)n
{
	NXRect r1;
	char tmp[200];
	
	if (fontObj) return self;
	
	PSgsave();
	PSrectclip(re -> origin.x,re -> origin.y,re -> size.width,re -> size.height);
	
	//NXEraseRect(re);
	[self getBounds:&r1];
	NXDrawWhiteBezel(&r1, re);
	NXSetColor(NX_COLORBLACK);
	[theFont set];
	
	PSrectclip(re -> origin.x + 2,re -> origin.y + 2,re -> size.width - 4,
		re -> size.height - 4);
	if (theStr) {
		sprintf(tmp,"%s %g pt. is %s",
				[theFont name], [theFont pointSize],theStr);
	} else {
		sprintf(tmp,"%s %g pt.",[theFont name],[theFont pointSize]);
	}
	PSmoveto(max(2.0, (bounds.size.width-[theFont getWidthOf:tmp])/2.0),
		max(2.0, (bounds.size.height - [theFont pointSize])/2.0));
	PSshow(tmp);
	//PScharpath(tmp,1);
	//PSfill();
	
	PSgrestore();
	PSflush();
	
	return self;
}

- font
{
    return fontObj ? [superview font] : theFont;
}

// proprietary extensions, RK, 10/31/94
id proposedFont; // Global
- proposedFont {return proposedFont;}
- acceptFont:font atPoint:(NXPoint *)p
{
    id tid;
    
	if (![self isEnabled]) return nil;
	proposedFont = font;
    if ([self action] && [self target]) {
		if ([self sendAction:[self action] to:[self target]] == nil)
			return self;
	}
	
    [self setFont:font];
    if (fontObj) {
		tid = [superview font];
	
		if ([tid matrix] != NX_IDENTITYMATRIX)
			tid = [Font newFont:[tid name] size:[tid pointSize] matrix:NX_IDENTITYMATRIX];
		
		if (font != tid) [[FontManager new] setSelFont:font isMultiple:NO];
    }
    
    return self;
}
	
- setFont:font
{
	if (![self isEnabled]) return nil;
    if ([font matrix] != NX_IDENTITYMATRIX)
	    font = [Font newFont:[font name] size:[font pointSize] matrix:NX_IDENTITYMATRIX];
    theFont = font;
    [self display];
    return self;
}
	
- mouseDown:(NXEvent *)ev
{
	 [DPFontPanel dragFont:(fontObj ? [superview font] : theFont) withEvent:ev fromView:self];
	
    return self;
}

- setFontObj:(id *)fo
{
    fontObj = fo;
    return self;
}

- setLastPreview:(char **)lp
{
    lastPreview = lp;
    return self;
}
- setStr:(NXAtom)newStr
{
	theStr=newStr;
	return self;
}

@end
